//
//  ICTossView.m
//  Iching
//
//  Created by Leonardo Celati on 18/01/10.
//  Copyright 2010 Web & Zen Multimedia. All rights reserved.
//

#import "ICTossView.h"
#import "IChingDef.h"


@implementation ICTossView


- (id)initWithToss:(NSMutableArray*)origin {
	//@synchronized(self) {
		response = origin;
		return [super initWithFrame:CGRectMake(TOSSVIEW_LEFT,TOSSVIEW_TOP,TOSSVIEW_WIDTH,TOSSVIEW_HEIGHT)];
	//}
}


- (void)drawRect:(CGRect)rect {
	//CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 0.0);
	//CGContextSetLineWidth(<#CGContextRef c#>, <#CGFloat width#>);
	//Get the CGContext from this view
	context = UIGraphicsGetCurrentContext();
	CGContextSetLineWidth(context, TOSSVIEW_BORDER_STROKE);
	CGContextAddRect(context, CGRectMake(TOSSVIEW_BORDER_LEFT,TOSSVIEW_BORDER_TOP,TOSSVIEW_BORDER_WIDTH,TOSSVIEW_BORDER_HEIGHT));
	CGContextStrokePath(context);
	
	for (int i=0;i<[response count];i++) {
		 
		switch ((Line)[[response objectAtIndex:i] intValue]) {
			case yinMutable:
				[self drawYinAtPos:i withMutation:TRUE];
				break;
			case yang:
				[self drawYangAtPos:i withMutation:FALSE];
				break;
			case yin:
				[self drawYinAtPos:i withMutation:FALSE];
				break;
			case yangMutable:
				[self drawYangAtPos:i withMutation:TRUE];
				break;

			default:
				NSLog(@"don't know...");
				break;
		}
	}	
} 

-(void)drawYinAtPos:(int)pos withMutation:(BOOL)mutation {
	NSLog(@"draw yin...");
	//Draw a rectangle mutable or not...
	if (mutation) {
		CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor);
	} else {
		CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
	}
	
	//Define two rectangles
	CGContextAddRect(context, CGRectMake(YIN_LLEFT, YIN_LTOP - (pos*LINE_MULTIPLIER), YIN_LWIDTH, YIN_LHEIGHT));
	CGContextAddRect(context, CGRectMake(YIN_RLEFT, YIN_RTOP - (pos*LINE_MULTIPLIER), YIN_RWIDTH, YIN_RHEIGHT));
	//Draw it
	CGContextFillPath(context);
}

-(void)drawYangAtPos:(int)pos withMutation:(BOOL)mutation {
	NSLog(@"draw yang...");
	//Draw a rectangle mutable or not...
	if (mutation) {
		CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor);
	} else {
		CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
	}
	//Define a rectangle
	CGContextAddRect(context, CGRectMake(YANG_LEFT, YANG_TOP - (pos*LINE_MULTIPLIER), YANG_WIDTH, YANG_HEIGHT));
	//Draw it
	CGContextFillPath(context);
}

- (void)dealloc {
    [super dealloc];
}


@end
